/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.model;

import edu.uci.ics.crawler4j.url.WebURL;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import utfpr.cp.crawler.Util;

/**
 *
 * @author Adriano_2
 */
@Entity
public class URLData implements Serializable {
    private static long serialVersionUID = 1L;

    /**
     * @return the serialVersionUID
     */
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    /**
     * @param aSerialVersionUID the serialVersionUID to set
     */
    public static void setSerialVersionUID(long aSerialVersionUID) {
        serialVersionUID = aSerialVersionUID;
    }
    @Id @GeneratedValue
    private Long id;
    @Column(length = 1000, unique = true)
    private String url;
    private String urldomain;
    private String subDomain;
    private String urlpath;
    private String anchor;
    @OneToOne(mappedBy = "url")
    private ResourceData resource;
    
    public URLData() {
    }
    
    public URLData(String url) {
        this.url = url;
    }

    public boolean exist() {
        return id != null;
    }
    
    public URLData(WebURL link) {
        url = link.getURL();
        urldomain = link.getDomain();
        subDomain = link.getSubDomain();
        urlpath = link.getPath();
        anchor = link.getAnchor();
    }
    
    @Override
    public String toString() {
        if (exist()) {
            return "utfpr.cp.model.URLData[ id=" + getId() + " ]";
        }
        else {
            return "utfpr.cp.model.URLData[ id=" + getUrl() + " ]";
        }
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = Util.maxLength(url, 1000);
    }

    /**
     * @return the urldomain
     */
    public String getDomain() {
        return urldomain;
    }

    /**
     * @param domain the urldomain to set
     */
    public void setDomain(String domain) {
        this.urldomain = Util.maxLength(domain, 256);
    }

    /**
     * @return the subDomain
     */
    public String getSubDomain() {
        return subDomain;
    }

    /**
     * @param subDomain the subDomain to set
     */
    public void setSubDomain(String subDomain) {
        this.subDomain = Util.maxLength(subDomain, 256);
    }

    /**
     * @return the urlpath
     */
    public String getPath() {
        return urlpath;
    }

    /**
     * @param path the urlpath to set
     */
    public void setPath(String path) {
        this.urlpath = Util.maxLength(path, 256);
    }

    /**
     * @return the anchor
     */
    public String getAnchor() {
        return anchor;
    }

    /**
     * @param anchor the anchor to set
     */
    public void setAnchor(String anchor) {
        this.anchor = Util.maxLength(anchor, 256);
    }

    /**
     * @return the resource
     */
    public ResourceData getPage() {
        return resource;
    }

    /**
     * @param page the resource to set
     */
    public void setPage(ResourceData page) {
        this.resource = page;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }    
}
